package com.qingcheng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.qingcheng.dao.AdditionalCommentMapper;
import com.qingcheng.dao.CommentMapper;
import com.qingcheng.service.comment.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import com.qingcheng.pojo.comment.Comment;
import com.qingcheng.pojo.comment.AdditionalComment;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentMapper commentMapper;
    /**
     * 根据Id查询该评论
     * @param id
     * @return
     */
    @Override
    public Comment findOneById(String id) {
        Comment comment = commentMapper.selectByPrimaryKey(id);
        return comment;}

    @Autowired
    private AdditionalCommentMapper additionalCommentMapper;

    @Override
    public List<Map<String, Object>> sort(Map searchMap) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("skuId",searchMap.get("skuId"));
        if(searchMap!=null){
            //晒图
            if(searchMap.get("isImg")!=null || "1".equals(searchMap.get("isImg"))){
                criteria.andEqualTo("isImg",searchMap.get("isImg"));
            }
            //追评
            if(searchMap.get("isAdd")!=null || "1".equals(searchMap.get("isAdd"))){
                criteria.andEqualTo("isAdd",searchMap.get("isAdd"));
            }

            //好评
            if(searchMap.get("level")!=null || "1".equals(searchMap.get("level"))){
              criteria.andEqualTo("level",searchMap.get("level"));
            }
            //中评
            if(searchMap.get("level")!=null || "2".equals(searchMap.get("level"))){
                criteria.andEqualTo("level",searchMap.get("level"));
            }
            //差评
            if(searchMap.get("level")!=null || "3".equals(searchMap.get("level"))){
                criteria.andEqualTo("level",searchMap.get("level"));
            }
        }
        List<Comment> commentList = commentMapper.selectByExample(example);
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap();
        for (Comment comment : commentList) {
            String id = comment.getId();
            Example example1 = new Example(AdditionalComment.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("commentId",id);
            List<AdditionalComment> additionalCommentList = additionalCommentMapper.selectByExample(example1);
            if(additionalCommentList.size()>0){
                AdditionalComment additionalComment = additionalCommentList.get(0);
                map.put("comment"+id,comment);
                map.put("additionalComment"+id,additionalComment);
                list.add(map);
            }else {
                map.put("comment"+id,comment);
                map.put("additionalComment"+id,null);
                list.add(map);
            }
        }
        return list;
    }
}
